package nebenlaeufig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class NGraph {
	
	private static ArrayList<Integer> nodes = new ArrayList<Integer>();
	private static Map<Integer,Integer> values = new HashMap<Integer,Integer>();
	private static Map<Integer,ArrayList<Integer>> relations = new HashMap<Integer,ArrayList<Integer>>();
	
	/**
	 * the constructor for this data structure
	 * @param filePaths where the XML-files are saved
	 * @throws Exception if one of the files have wrong format
	 */
	public NGraph(String[] filePaths) throws Exception {
		NParser parser = new NParser(filePaths);
		
		nodes = parser.getNodes();
		values = parser.getValues();
		relations = parser.getRelations();
	}
	
	/**
	 * to get the nodes of the graph after parsing
	 * @return the nodes
	 */
	public ArrayList<Integer> getNodes(){
		return nodes;
	}
	
	/**
	 * to get the values of the nodes after parsing
	 * @return the values
	 */
	public Map<Integer,Integer> getValues(){
		return values;
	}
	
	/**
	 * to get the relations between the nodes after parsing
	 * @return
	 */
	public Map<Integer,ArrayList<Integer>> getRelations(){
		return relations;
	}
	/**
	 * 
	 * @param node
	 * @return
	 */
	/**
	 * calculate the parents of one specific node n
	 * @param node n
	 * @return the parents
	 */
	public static NMesh Pre(int node){
		NMesh erg = new NMesh();
		
		ArrayList<Integer> actualRel;
		int actualNode;
		for(int i=0;i<nodes.size();i++){
			actualNode = nodes.get(i);
			actualRel = relations.get(actualNode);
			if(actualRel==null)
				continue;
			if(actualRel.contains(node))
				erg.add(actualNode);
		}
		return erg;
	}
	
	/**
	 * calculate the parents of a node group m
	 * @param mesh m
	 * @return the parents
	 */
	public static NMesh Pre(NMesh mesh){
		NMesh erg = new NMesh(),
			temp;
		for(int i=0;i<mesh.size();i++){
			temp = Pre(mesh.get(i));
			for(int j=0;j<temp.size();j++){
				erg.add(temp.get(j));
			}
		}
		return erg;
	}
}